package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.SysUserCompRel;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.StringUtils;

/**
 * 用户-企业
 * @author Administrator
 *
 */
@Service
public class SysUserCompRelService extends BaseService<SysUserCompRel, String> {

	/**
	 * 添加用户企业关系
	 * @param userid
	 * @param companyid
	 * @param loginUser
	 * @return
	 */
	public SysError addUserCompanyRel(String userid, String companyid, LoginUser loginUser){
		if(StringUtils.isEmpty(userid) || StringUtils.isEmpty(companyid)){
			return null;
		}
		Object id = getByHql("select id from SysUserCompRel where sysUser.userid=? and company.companyid=?", userid, companyid);
		if(id != null){
			return null;
		}
		SysUserCompRel rel = new SysUserCompRel(new Company(companyid), new SysUser(userid), loginUser.getUsername(), new Date());
		this.save(rel);
		return null;
	}
	
	
	/**
	 * 设置用户的企业查看权限
	 * @param userid
	 * @param companyids
	 * @param loginUser
	 * @return
	 */
	public SysError saveUserCompanyRels(String userid, String[] companyids, LoginUser loginUser){
		if(StringUtils.isEmpty(userid)){
			return null;
		}
		//删除现有关联
		deleteByUserid(userid);
		
		if(companyids==null || companyids.length==0){
			return null;
		}
		
		Date now = new Date();
		String createBy = loginUser.getLogin_name();
		
		for(String companyid : companyids){
			SysUserCompRel rel = new SysUserCompRel(new Company(companyid), new SysUser(userid), createBy, now);
			this.save(rel);
		}
		
		return null;
	}
	
	
	/**
	 * 获取指定用户关联的企业id集合
	 * @param userid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Set<String> getUserCompanyids(String userid){
		Set<String> set = new HashSet<String>();
		if(StringUtils.isEmpty(userid) ){			
			return set;
		}
		
		List<String> list = listByHql("select company.companyid from SysUserCompRel where sysUser.userid=?", userid);
		if(list != null){
			set.addAll(list);
		}
		return set;
	}
	
	
	/**
	 * 删除用户企业关联关系
	 * @param userid
	 * @return
	 */
	public SysError deleteByUserid(String userid){
		deleteByHql("delete from SysUserCompRel where sysUser.userid=?", userid);
		return null;
	}
	
	
	/**
	 * 获取指定企业关联的企业用户
	 * @param companyid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysUser> getCompanyUserData(String companyid){
		List<SysUser> list = listByHql("select new com.qingxin.sys.entity.SysUser(u.userid, u.login_name, u.username)" +
				" from SysUser u where u.user_type='2'" +
				" and u.userid in (select distinct v.sysUser.userid from SysUserCompRel v where v.company.companyid=?)" +
				" order by u.username asc", companyid);
		if(list == null){
			return new ArrayList<SysUser>();
		}
		return list;
	}
	
	
	
	
	
}
